package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.search.query.ExpressionParser.negation_return;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DutyBean;
import com.ray.bean.MaterialTrunoverBean;
import com.ray.bean.ReportBean;
import com.ray.bean.TableData;
import com.ray.bean.TurnoverBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.WorkshopService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/workshop")
public class WorkshopController {
	
	@Autowired
	WorkshopService workshopService;
	
	/**
	 * 获取可审核报工记录的人
	 * @param request
	 * @return
	 */
//	@RequestMapping("/getCanApprovalReportUser")
//	@ResponseBody
//	public Map getCanApprovalReportUser(HttpServletRequest request){
//		Map map=RequestUtil.getRequestAllParam(request);
//		return ResponseUtil.ResponseResult(workshopService.getCanApprovalReportUser(map));
//	}
	
	
	
//	从车间生产周期表获取所有生产线
	@RequestMapping("/getProductionLineFromCycle")
	@ResponseBody
	public Map getProductionLineFromCycle(){
		return ResponseUtil.ResponseResult(workshopService.getProductionLineFromCycle());
	}
	
//	通过车间名称获取车间生产周期详情
	@RequestMapping("/getWorkshopProductionDetail")
	@ResponseBody
	public Map getWorkshopProductionDetail(HttpServletRequest request,String workshopName,String productionLine) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkshopProductionDetail(map));
	}
	
//	获取所有车间名称
	@RequestMapping("/getWorkshopName")
	@ResponseBody
	public Map getWorkshopName(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkshopName(map));
	}
	
//	获取该车间下所有工序名称
	@RequestMapping("/getWorkingProcedureName")
	@ResponseBody
	public Map getWorkingProcedureName(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkingProcedureName(map));
	}
	
	/**
	 * 查询产能信息
	 * @param request
	 * @param deptId(部门id)
	 * @param workShopId(车间id)
	 * @param month(月份)
	 * @return
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@RequestMapping("/queryCapacity")
	@ResponseBody
	public Map queryCapacity(HttpServletRequest request) throws ServiceException, ParseException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.queryCapacityFromRedis(map));
	}
	
	//查询产能信息,根据车间与时间
	@RequestMapping("/getTableData")
	@ResponseBody
	public DatatablesViewPageBean getTableData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getTableData(map);
	}
	
	/***
	 * 获取人员产能信息
	 * @param workShop(车间id)
	 * @param date(结束时间)
	 * @param startDay(开始时间)
	 * @param searchingWhere(搜索条件)
	 * @return
	 */
	@RequestMapping("/getPersonData")
	@ResponseBody
	public Map getPersonData(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		 return ResponseUtil.ResponseResult(workshopService.getPersonData(map));
	}

	
	//查询产能信息,根据车间与工序
	@RequestMapping("/getTotal")
	@ResponseBody
	public Map getTotal(String deptId,String date,String startDay,HttpServletRequest request) throws ServiceException, ParseException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(workshopService.getTotal(deptId,date,startDay,myUser));
	}
	
	//查询尾数拖期异常
	@RequestMapping("/getDutyDate")
	@ResponseBody
	public DatatablesViewPageBean getDutyDate(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getDutyDate(map);
	}
	
	//查询尾数拖期异常
	@RequestMapping("/getDuty")
	@ResponseBody
	public Map getDuty(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getDuty(map));
	}
	
	//查询每个车间尾数达成率异常
	@RequestMapping("/getQualified")
	@ResponseBody
	public Map getQualified(String date,HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getQualified(map));
	}
	
	//查询每个车间拖期订单
	@RequestMapping("/getDelayDate")
	@ResponseBody
	public DatatablesViewPageBean<DutyBean> getDelayDate(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getDelayDate(map);
	}
	
	//查询每个车间拖期订单
	@RequestMapping("/getOnTime")
	@ResponseBody
	public Map getOnTime(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getOnTime(map));
	}
	
	
	//查询每个车间七天内拖期订单数
	@RequestMapping("/getSereven")
	@ResponseBody
	public Map getSereven(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getSereven(map));
	}
	
	//查询每个车间七天内拖期订单数
	@RequestMapping("/getSoonDelay")
	@ResponseBody
	public Map getSoonDelay(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getSoonDelay(map));
	}
	
	//获取不合格原因
	@RequestMapping("/getNoReason")
	@ResponseBody
	public Map getNoReason(String workShopName,String produce) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getNoReason(workShopName,produce));
	}
	
	//获取周转量数据
	@RequestMapping("/getTrunover")
	@ResponseBody
	public Map getTrunover(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getTrunover(map));
	}
	
	
	//获取周转量数据
	@RequestMapping("/getTrunoverNew")
	@ResponseBody
	public DatatablesViewPageBean<TurnoverBean> getTrunoverNew(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return workshopService.getTrunoverNew(map);
	}
	
	//获取周转量明细数据
	@RequestMapping("/getTrunoverNewDetail")
	@ResponseBody
	public DatatablesViewPageBean<TurnoverBean> getTrunoverNewDetail(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return workshopService.getTrunoverNewDetail(map);
	}
	
	
	//车间遗留物料数据
	@RequestMapping("/getWorkLeftOver")
	@ResponseBody
	public DatatablesViewPageBean<TurnoverBean> getWorkLeftOver(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return workshopService.getWorkLeftOver(map);
	}

	//查询每个车间周转量
	@RequestMapping("/getDeptTurnover")
	@ResponseBody
	public Map getDeptTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
 		return ResponseUtil.ResponseResult(workshopService.getDeptTurnover(map));
	}
	
	//查询车间七日周转量
	@RequestMapping("/serverTurnover")
	@ResponseBody
	public Map serverTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.serverTurnover(map));
	}
	
	//获取厂家
	@RequestMapping("/getFactory")
	@ResponseBody
	public Map getFactory(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getFactory(workShopName));
	}
	
	//获取设备编号
	@RequestMapping("/getDeviceId")
	@ResponseBody
	public Map getDeviceId(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getDeviceId(workShopName));
	}
	
	//获取设备名称
	@RequestMapping("/getDeviceName")
	@ResponseBody
	public Map getDeviceName(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getDeviceName(workShopName));
	}
	
	//获取审核订单
	@RequestMapping("/getShenHeOrder")
	@ResponseBody
	public Map getShenHeOrder(String userName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getShenHeOrder(userName));
	}
	
	
	//审核删除订单
//	@RequestMapping("/deleteOder")
//	@ResponseBody
//	public Map deleteOder(String id) throws ServiceException{
//		workshopService.deleteOder(id);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
//	}
	
	//审核订单
//	@RequestMapping("/shenHeOrder")
//	@ResponseBody
//	public Map shenHeOrder(String userName,String id) throws ServiceException{
//		workshopService.shenHeOrder(userName,id);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.APPROVAL_SUCCESS);
//	}
	
	//获取前十条不合格工序
	@RequestMapping("/getTopProcedureException")
	@ResponseBody
	public Map getTopProcedureException(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getTopProcedureException(map));
	}
	
	//获取前十条不合格原因
	@RequestMapping("/getTopNoReasonException")
	@ResponseBody
	public Map getTopNoReasonException(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getTopNoReasonException(map));
	}
	
	//获取整厂周转量
	@RequestMapping("/getFactoryTurnover")
	@ResponseBody
	public Map getFactoryTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getFactoryTurnover(map));
	}
	
	
	//获取部门报工加工对象
	@RequestMapping("/getMachining")
	@ResponseBody
	public Map getMachining(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getMachining(map));
	}
	
	
	//通过页面报工
	@RequestMapping("/insertReportByPage")
	@ResponseBody
	public Map insertReportByPage(String reportArrayJSONStr,String reportUser,HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		workshopService.insertReport(reportArrayJSONStr, reportUser,myUser);
		return ResponseUtil.ResponseResult(ReturnStringUtil.REPORT_SUCCESS);
	}
	
	//发起修改报工申请
	@RequestMapping("/launchUpdateReport")
	@ResponseBody
	public Map launchUpdateReport(HttpServletRequest request,String id) throws ServiceException, CloneNotSupportedException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		workshopService.launchUpdateReport(id,myUser);
		return ResponseUtil.ResponseResult(ReturnStringUtil.LAUNCH_SUCCESS);
	}
	
	
	//拒绝修改申请
	@RequestMapping("/notLaunchReport")
	@ResponseBody
	public Map notLaunchReport(HttpServletRequest request,String id) throws ServiceException, CloneNotSupportedException{
		workshopService.notLaunchReport(id);
		return ResponseUtil.ResponseResult(ReturnStringUtil.LAUNCH_RETURN_SUCCESS);
	}
	
	//通过页面报工
	@RequestMapping("/getWorkShopMaterialSumPrice")
	@ResponseBody
	public Map getWorkShopMaterialSumPrice(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkShopMaterialSumPrice(map));
	}
	/**
	 * 获取订单物料单位
	 * @param orderId(订单编号)
	 * @param machining(加工对象)
	 * @return
	 */
	@RequestMapping("/getMaterialUnit")
	@ResponseBody
	public Map getMaterialUnit(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getMaterialUnit(map));
	}
	
	
	
	/**
	 * 获取报工公司
	 * @return
	 */
	@RequestMapping("/getAllReportCompanyData")
	@ResponseBody
	public Map getAllReportCompanyData(HttpServletRequest request) {
		Map map =RequestUtil.getRequestAllParamNotLogin(request);
		return ResponseUtil.ResponseResult(workshopService.getAllReportCompanyData(map));
	}
	
	/**
	 * 获取制单公司
	 * @return
	 */
	@RequestMapping("/getCreateOrderCompanyData")
	@ResponseBody
	public Map getCreateOrderCompanyData(HttpServletRequest request) {
		Map map =RequestUtil.getRequestAllParamNotLogin(request);
		return ResponseUtil.ResponseResult(workshopService.getCreateOrderCompanyData(map));
	}
	
	
	
	//查询每个车间拖期订单
	@RequestMapping("/getWorkShopMaterial")
	@ResponseBody
	public DatatablesViewPageBean<MaterialTrunoverBean> getWorkShopMaterial(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getWorkShopMaterial(map);
	}
	
	
	//查询每个车间拖期订单
	@RequestMapping("/getWorkShopMaterialNotCode")
	@ResponseBody
	public DatatablesViewPageBean<MaterialTrunoverBean> getWorkShopMaterialNotCode(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getWorkShopMaterialNotCode(map);
	}
	
	
	/**
	 * 查询日产能详情
	 * @param workShopId(车间id)
	 * @param day(日期)
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/dayCapacityDetail")
	@ResponseBody
	public Map dayCapacityDetail(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.dayCapacityDetail(map));
	}
	
	
	
	/**
	 * 设置车间订单异常理由
	 * @param orderId(订单编号)
	 * @param workShopId(车间id)
	 * @param reason(拖期理由)
	 * @return
	 */
	@RequestMapping("/setWorkShopOrderExceptionReason")
	@ResponseBody
	public Map setWorkShopOrderExceptionReason(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		workshopService.setWorkShopOrderExceptionReason(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	//下载员工达成率数据
	@RequestMapping(value="/downlodeReachFile",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeReachFile(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<TableData> datatablesViewPageBean=workshopService.getTableData(map);
		List<TableData> tableList=datatablesViewPageBean.getReturnData();
		String [] tableHeadFields={"姓名","订单编号","工厂型号","客户订单号","作业工序","作业产能","作业单付时间(秒)","有效工作时间(时)","工作效率","报工日期","单价","总价"};
		String [] fields={"name","companyOrderId","factoryId","coustomId","procedur","capacity","oneTime","effectiveTime","workEffective","workDate","price","sumPrice"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "员工达成率.xlsx", true);
	}
	
	
	//下载车间尾数数据
	@RequestMapping(value="/downlodeMantissa",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeMantissa(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<DutyBean> datatablesViewPageBean=workshopService.getDutyDate(map);
		List<DutyBean> tableList=datatablesViewPageBean.getReturnData();
		String [] tableHeadFields={"订单编号","客户型号","工厂型号","颜色描述","订单数量","投料数","入库数","出库数","短出数","合格数量","不良品数","缺数","车间","尾数造成工序","尾数造成人","不合格原因"};
		String [] fields={"companyOrderId","customerId","factoryId","color","orderCount","feddingCount","inCount","outCount", "shortCount","qualifiedCount","noQualifiedCount","lackCount","workShopName","procedur","userName","noQuealtyReasonList"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "尾数.xlsx", true);
	}
	
	//下载车间拖期数据
	@RequestMapping(value="/downlodeDalay",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeDalay(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<DutyBean> datatablesViewPageBean=workshopService.getDelayDate(map);
		List<DutyBean> tableList=datatablesViewPageBean.getReturnData();
		String [] tableHeadFields={"订单编号","客户型号","工厂型号","颜色描述","订单数量","交接入库时间","有效出库时间","拖期时间","产品良品数"};
		String [] fields={"companyOrderId","customerId","factoryId","color","orderCount","inWorkShopTime","outWorkShopTime","delayTime","qualifiedCount"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "拖期.xlsx", true);
	}
	
//	//下载车间产能数据
//	@RequestMapping(value="/downlodeCapaCity",method=RequestMethod.GET)
//	@ResponseBody
//	public void downlodeCapaCity(String date,HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		String titleList=map.get("listString").toString();
//		JSONArray jsonArray = JSONArray.fromObject(titleList);
//		List<String> titleLists=jsonArray.subList(0, jsonArray.size());
//		List<Map> tableList=workshopService.queryCapacity(map);
//		String [] tableHeadFields={"订单编号","工厂型号","客户型号","产品颜色","订单数量"};
//		String [] fields={"dingDanId","factoryId","keHu","color","dingDanShu"};
//		tableHeadFields=workshopService.getArrayHead(tableHeadFields,titleLists,"合格","不合格");
//		fields=workshopService.getArrayHead(fields,titleLists,"","no");
//		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
//		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
//		ResponseUtil.downloadfile(response, excelFile, "产能.xlsx", true);
//	}
	
	
	//下载车间周转量数据
	@RequestMapping(value="/downlodeTurnover",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeTurnover(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
	    Map map =RequestUtil.getRequestAllParam(request);
	    map.put("start", null);
		map.put("length", null);
//	    List<String> titleLists=workshopService.getWorkingProcedureName(map);
		DatatablesViewPageBean<TurnoverBean> ddBean=workshopService.getTrunoverNew(map);
		List<TurnoverBean> tableList=ddBean.getReturnData();
		String [] tableHeadFields={"系统订单编号","公司订单编号","工厂型号","客户型号","客户订单号","加工对象","人工排期","入库时间","入库数","出库数","周转量"};
		String [] fields={"orderId","companyOrderId","factoryId","customerId","customerOrderId","machining","setTime","inTime","inCount","outCount","turnover"};
//		tableHeadFields=workshopService.getArrayHead(tableHeadFields,titleLists,"框未完成","框不良品","腿未完成","腿不良品","报工时间");
//		fields=workshopService.getArrayHead(fields,titleLists,"kuangTurnover","kuangNoQualified","tuiTurnover","tuiNoQualified","lastTime");
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "周转量.xlsx", true);
	}
	
	
	
	
}
